<template>
  <div class="page">
    <div class="weui-flex pay_top">
      <div class="weui-flex__item">需支付</div>
      <div>¥{{params.price||params.amount||price}}</div>
    </div>
    <div class="weui-cells">
       <div class="weui-cell payitem">
        <div class="weui-cell__bd">请选择支付方式</div>
      </div>
      <div class="weui-cell payitem">
        <div class="weui-cell__hd"><img src="../../../static/images/weixin.png"></div>
        <div class="weui-cell__bd">微信支付</div>
        <div class="weui-cell__ft">
          <van-icon :name="payWx"  @click="payWxFun" size='1.3em' :color="payWx=='circle'?'':'#FF787E'"/>
          </div>
      </div>
      <div class="weui-cell payitem" v-if="isTuangou!=2">
        <div class="weui-cell__hd"><img src="../../../static/images/yue.png"></div>
        <div class="weui-cell__bd">余额支付</div>
        <div class="weui-cell__ft">
          <van-icon :name="payYe"  @click="payYeFun" size='1.3em' :color="payYe=='circle'?'':'#FF787E'"/>
          </div>
      </div>
    </div>
    <div class="footer">
      <a @click="pay">确认支付 ￥{{params.price||params.amount||price}}</a>
    </div>
  </div>
</template>

<script>
    import * as appId from "@/utils/util"
     import * as appSecret from "@/utils/util"
export default {
  data () {
    return {
      params:{},
      payWx:"checked",
      payYe:"circle",
      isTuangou:0,
      status:0,
      openid:"",
      groupId:'',
      orderId:"",
      price:0
    }
  },
  onUnload(){
    wx.removeStorageSync('groupId')
    wx.removeStorageSync('payParams')
  },
 onLoad(option){
      console.log(option)
      this.orderId=option.orderId;
      this.price=option.price;
    },
    onShow(){
      wx.setNavigationBarTitle({
        title: "支付"
      })
      let that=this;

      this.params= wx.getStorageSync('payParams')&& JSON.parse(wx.getStorageSync('payParams'))
       this.userInfo =wx.getStorageSync('userInfo')&&JSON.parse(wx.getStorageSync('userInfo'))
       this.openid=wx.getStorageSync('openid');
        this.isTuangou=wx.getStorageSync('isTuangou');
        this.status=  wx.getStorageSync('tStatus')//开团
      this.groupId=wx.getStorageSync('groupId')
    },
  methods: {
    payYeFun(){
       if(this.payYe=='circle'){
        this.payWx='circle'
        this.payYe='checked'
      }
    },
    payWxFun(){
        if(this.payWx=='circle'){
        this.payWx='checked'
          this.payYe='circle'
      }
    },
    wxPay(params){
      params.buyStatus=0;
        this.$http.request({
                method:"post",
                url:"/v1.0/appletOperat/generatingOrders",
                body:params
      }).then((data)=>{
        let result=data.result;
         this.wxComment(result)
      })

    },
    yuPay(params){//余额支付
        params.buyStatus=1;
         this.$http.request({
                 method:"post",
                 url:"/v1.0/appletOperat/generatingOrders",
                 body:params
       }).then((yuData)=>{
        if(yuData.code==0){
             wx.showToast({
                      title: `${yuData.message}`,
                      icon: 'success',
                      duration: 1500,
                      success:()=>{
                        if(that.isTuangou==0||that.isTuangou==3){//单独支付
                          wx.removeStorageSync('payParams')

                          let url=`/pages/alignPaySuccess/main`;
                           wx.reLaunch({ url})
                        }else if(that.isTuangou==1){//拼团支付
                          wx.removeStorageSync('payParams')

                          let url=`/pages/success/main`;
                           wx.reLaunch({ url})
                        }
                      }
                  })
        }else{
            wx.showToast({
                     title: `${yuData.message}`,
                     icon: 'none',
                     duration: 1500
                 })
        }
      })
    },
    wxComment(result,prepay_id){//微信支付
    let that=this;
       wx.requestPayment({
        timeStamp:  result.timeStamp,
        nonceStr: result.nonceStr,
        package: result.package,
        signType: 'MD5',
         "appid":appId.appId,
        paySign: result.sign,
        appId:appId.appId,
        success (res) {
           wx.showToast({
                        title: '支付成功！',
                        icon: 'success',
                        duration: 2000,
                        success:(res)=>{
                          if(that.isTuangou==0||that.isTuangou==3){//单独支付
                            wx.removeStorageSync('payParams')
                              wx.removeStorageSync('cartData')
                            let url=`/pages/alignPaySuccess/main`;
                             wx.reLaunch({ url})
                          }else if(that.isTuangou==2){//充值
                           wx.removeStorageSync('payParams')
                          let url=`/pages/rechargesuccess/main?id=${prepay_id}`
                          wx.reLaunch({ url })
                         }
                        }
                    })

        },
        fail (res) {

           wx.showToast({
                        title: "支付失败！",
                        icon: 'none',
                        duration: 1500
                    })

        }
      })
    },
    pay(){

      let that=this;
      let params={
        "userId":that.userInfo.baseUserId,
        "phone":that.userInfo.mobileNo,
        "appid":appId.appId,
      }


         params=Object.assign(params,this.params)
          if(this.isTuangou==3){

             this.$http.request({
                      method:"post",
                      url:"/v1.0/appletOperat/wxPay",
                      body:{
                        "userId":that.userInfo.baseUserId,
                        "phone":that.userInfo.mobileNo,
                        "appid":appId.appId,
                        openid:this.openid,
                        	"orderId":this.orderId,
                           buyStatus:this.payWx=="checked"?0:this.payYe=="checked"?1:"",
                      }
            }).then((data)=>{
              if(data.code==0){
                 let result=data.result;
                   this.wxComment(result)
              }else{
                wx.showToast({
                         title: `${data.message}`,
                         icon: 'none',
                         duration: 1500
                     })
              }

            })
          }else
        if(this.isTuangou==2){//充值
        console.log('充值')
          let czParams={
            	"appSecret":appSecret.appSecret,

          }
          czParams=Object.assign(czParams,params)
               this.$http.request({
                      method:"post",
                      url:"/v1.0/appletHome/appletRecharge",
                      body:czParams
            }).then((czdata)=>{
              if(czdata.code==0){
                               let result=czdata.result;
                  if(this.payWx=="checked"){
                    this.wxComment(result,result.orderNo)
                  }
                  // if(this.payYe=="checked"){
                  //  this.yuPay(params)
                  // }
              }else{
                  wx.showToast({
                           title: `${czdata.message}`,
                           icon: 'none',
                           duration: 1500
                       })
              }
            })
        }else
         if(this.isTuangou==0){//单独支付


                params.openid=this.openid;

                 if(this.payWx=="checked"){
                    this.wxPay(params);
                 }
                 if(this.payYe=="checked"){
                  this.yuPay(params)
                 }

         }else{//拼团支付
           //wx.login({
           // wx.login({
           //   success(res) {
           //     console.log(res)
           //   }
               let pinParams={
                 productId: that.params.productId,
                 buyStatus:that.payWx=="checked"?0:that.payYe=="checked"?1:"",
                 appSecret:appSecret.appSecret,
                 status:that.status,
                 openId:res.code,
                 groupid:that.status==2?that.groupId:""
               }
               pinParams=Object.assign(pinParams,params)
               console.log(pinParams)

               that.$http.request({
                 method:"post",
                 url:"/v1.0/appletOperat/groupBuying",
                 body:pinParams
               }).then((pingData)=>{
                 if(pingData.code==0){

                   let result=pingData.result;
                   wx.requestPayment({
                     timeStamp: result.timeStamp,
                     nonceStr: result.nonceStr,
                     package: result.package,
                     signType: 'MD5',
                     paySign: result.sign,
                     success (res) {
                       wx.showToast({
                         title: '支付成功！',
                         icon: 'success',
                         duration: 2000,
                         success:()=>{
                           wx.removeStorageSync('payParams')
                           wx.removeStorageSync('productDetail')
                           let url=`/pages/success/main`
                           wx.reLaunch({ url })
                         }
                       })

                     },
                     fail (res) {
                       wx.showToast({
                         title: "支付失败！",
                         icon: 'none',
                         duration: 1500
                       })

                     }
                   })
                   //  if(this.payWx=="checked"){
                   //    this.wxPay(result,params);
                   // }
                   // if(this.payYe=="checked"){
                   //  this.yuPay(result,params)
                   // }
                 }else{
                   wx.showToast({
                     title: `${pingData.message}`,
                     icon: 'none',
                     duration: 1500
                   })
                 }
               })

             // },
           // })


         }




    }
  },

  created () {
    // let app = getApp()
  }
}
</script>

<style scoped>
.page{
  padding-bottom: 3.5em;
}
.pay_top{
  background: #FF787E;
  padding: 0.8em;
  color: #fff;
  font-size: 0.8em;
}
.payitem{
  font-size: 0.9em;
}
.payitem img{
  width: 1.8em;
  height: 1.8em;
  margin-right: 0.5em;
}
.footer{
  position: fixed;
  bottom: 0px;
  left: 0px;
  z-index: 9999;
  right: 0px;
  background: #fff;
  padding: 0.6em 2em;
  border-top: 1px #eee solid;
}
.footer a{
  display: block;
  height: 2.4em;
  line-height: 2.4em;
  color: #fff;
  text-align: center;
  font-size: 0.9em;
  background: #FF787E;
   border-radius: 1.2em;
}
</style>
